package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Repairrequest;
import models.Driver;
@Entity
@Table(name="repair")
public class Repair extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@ManyToOne
	public Repairrequest repairrequest; //维修申报编号
	@ManyToOne
	public Driver driver; //司机
	
	public String repair_type; //维修类别
	
	public Integer mileage; //维修里程
	
	public Integer salary_fee; //人工费
	
	public Integer material_fee; //材料费
	
	public Integer manger_fee; //管理费
	
	public Integer total_fee; //维修费合计
	
	public Integer insurance_pay; //定损金额
	
	public Integer self_pay; //自负金额
	public static Model.Finder<Integer, Repair> find = new Model.Finder<Integer, Repair>(Integer.class, Repair.class);

	public static Page<Repair> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Repair> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setRepairrequest(Repairrequest d) {
		repairrequest= d;
	}
	public Repairrequest getRepairrequest() {
		return repairrequest;
	}
	public void setDriver(Driver d) {
		driver= d;
	}
	public Driver getDriver() {
		return driver;
	}
	public void setRepair_type(String d) {
		repair_type= d;
	}
	public String getRepair_type() {
		return repair_type;
	}
	public void setMileage(Integer d) {
		mileage= d;
	}
	public Integer getMileage() {
		return mileage;
	}
	public void setSalary_fee(Integer d) {
		salary_fee= d;
	}
	public Integer getSalary_fee() {
		return salary_fee;
	}
	public void setMaterial_fee(Integer d) {
		material_fee= d;
	}
	public Integer getMaterial_fee() {
		return material_fee;
	}
	public void setManger_fee(Integer d) {
		manger_fee= d;
	}
	public Integer getManger_fee() {
		return manger_fee;
	}
	public void setTotal_fee(Integer d) {
		return;
	}
	public Integer getTotal_fee() {
		return  salary_fee+material_fee+manger_fee;
	}
	public void setInsurance_pay(Integer d) {
		insurance_pay= d;
	}
	public Integer getInsurance_pay() {
		return insurance_pay;
	}
	public void setSelf_pay(Integer d) {
		self_pay= d;
	}
	public Integer getSelf_pay() {
		return self_pay;
	}
	public static Repair findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Repair v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
}
